package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/3/13 10:56
 */
@WebServlet("/login")
@SuppressWarnings({"all"})
public class LoginServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 用于处理登录时的 post 请求, 验证账号密码是否正确
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8"); // 让 servlet 程序也使用 utf8 的方式解析请求, 避免解析中文时出错
        // 1. 获取请求中的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username == null || "".equals(username) || password == null || "".equals(password)){
            // 处理用户名和密码为空的情况
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户名或密码为空!");
            return;
        }
        // 2. 与数据库中的账号密码进行比对
        UserDao userDao = new UserDao();
        User user = userDao.selectByName(username);
        if (user == null || !user.getPassword().equals(password)){
            // 用户名不存在或者密码错误
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("用户名不存在或密码错误!");
            return;
        }
        // 3. 如果比对通过则创建会话
        HttpSession session = req.getSession(true);
        // 把刚才的用户信息存储到会话中, 这里存储 user 对象
        session.setAttribute("user", user);
        // 4. 返回一个重定向报文, 跳转到博客列表页
        resp.sendRedirect("blog_list.html");
    }

    /**
     * 用于让前端检测用户当前的登录状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 检测会话是否存在, 如果为 null 则未登录
            User user = new User();
            resp.getWriter().write(objectMapper.writeValueAsString(user));
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            // 如果有会话但是 user 为null, 也视为未登录
            user = new User();
            resp.getWriter().write(objectMapper.writeValueAsString(user));
            return;
        }
        // 此时已经是登录的状态, 但是需要注意, 不要把密码返回给前端
        user.setPassword("");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }
}
